//
//  XKNewsHeaderSelectView.m
//  XKJY
//
//  Created by xuyaguang on 2017/1/11.
//  Copyright © 2017年 xiaokang100. All rights reserved.
//

#import "XKNewsHeaderSelectView.h"

@interface XKNewsHeaderSelectView ()

@property (nonatomic, strong) UIView *redLineView;
@property (nonatomic, strong) NSMutableArray<UIButton *> *btnArrM;

@end

@implementation XKNewsHeaderSelectView

#pragma mark - 重写构造方法
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self configArguments];
        [self createSubviews];
        [self addConstraints];
    }
    return self;
}

#pragma mark - 设置自身
- (void)configArguments {
    self.backgroundColor = kBaseGrayColor;
}

#pragma mark - 创建控件
- (void)createSubviews {
    _leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _leftBtn.titleLabel.font = Font(15);
    _leftBtn.tag = 1;
    _leftBtn.selected = YES;
    [_leftBtn setTintColor:ClearColor];
    [_leftBtn setTitle:@"--" forState:UIControlStateNormal];
    [_leftBtn setTitleColor:BlackColor forState:UIControlStateNormal];
    [_leftBtn setTitleColor:RedColor forState:UIControlStateSelected];
    [_leftBtn setBackgroundColor:WhiteColor];
    [_leftBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    _rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _rightBtn.titleLabel.font = Font(15);
    _rightBtn.tag = 2;
    [_rightBtn setTintColor:ClearColor];
    [_rightBtn setTitle:@"--" forState:UIControlStateNormal];
    [_rightBtn setTitleColor:BlackColor forState:UIControlStateNormal];
    [_rightBtn setTitleColor:RedColor forState:UIControlStateSelected];
    [_rightBtn setBackgroundColor:WhiteColor];
    [_rightBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    _redLineView = [UIView new];
    _redLineView.backgroundColor = RedColor;
    
    [self addSubview:self.leftBtn];
    [self addSubview:self.rightBtn];
    [self addSubview:self.redLineView];
    
    [self.btnArrM addObject:self.leftBtn];
    [self.btnArrM addObject:self.rightBtn];
    
}

#pragma mark - 按钮点击事件
- (void)btnClick:(UIButton *)sender {

    @weakify(self);
    [_btnArrM enumerateObjectsUsingBlock:^(UIButton * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        @strongify(self);
        [self.redLineView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(self.mas_left).offset(Main_Width*(sender.tag*0.5 - 0.25));
        }];
        if (obj == sender) {
            obj.selected = YES;
        } else {
            obj.selected = NO;
        }
    }];
    
    if ([self.delegate respondsToSelector:@selector(btnClickWithBtn:)]) {
        [self.delegate btnClickWithBtn:sender];
    }
}

#pragma mark - 外部方法
- (void)leftBtnClick {
    [self btnClick:self.leftBtn];
}

- (void)rightBtnClick {
    [self btnClick:self.rightBtn];
}


#pragma mark - 添加约束
- (void)addConstraints {
    
    [self.leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.width.mas_equalTo(Main_Width/2);
        make.top.mas_equalTo(self.mas_top).offset(5*Scale_H);
        make.bottom.mas_equalTo(self.mas_bottom).offset(-1*Scale_H);
    }];
    
    [self.rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(0);
        make.width.mas_equalTo(Main_Width/2);
        make.top.mas_equalTo(self.mas_top).offset(5*Scale_H);
        make.bottom.mas_equalTo(self.mas_bottom).offset(-1*Scale_H);
    }];
    
    [self.redLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(Main_Width/2, 2*Scale_H));
        make.centerX.mas_equalTo(self.mas_left).offset(Main_Width/4);
        make.bottom.mas_equalTo(self).offset(0*Scale_H);
    }];
}

- (NSMutableArray<UIButton *> *)btnArrM {
    if (!_btnArrM) {
        _btnArrM = [NSMutableArray array];
    }
    return _btnArrM;
}

@end
